#include "aed_archtype.h"

AED_Archtype::AED_Archtype(const QString &name):m_name(name)
{

}

AED_Archtype::AED_Archtype(const AED_Archtype &arch){
    m_name = arch.m_name;
    m_rendercomp = arch.m_rendercomp;
    m_keyboardcomp = arch.m_keyboardcomp;
    m_physicscomp = arch.m_physicscomp;
    m_alwayscomp = arch.m_alwayscomp;
    m_mousecomp = arch.m_mousecomp;
    m_parsyscomp = arch.m_parsyscomp;
    m_particlecomp = arch.m_particlecomp;
    m_soundcomp = arch.m_soundcomp;

    m_triggers = arch.m_triggers;
    m_attributes = arch.m_attributes;

}

QString AED_Archtype::GetName() const{
    return m_name;
}

void AED_Archtype::SetName(const QString &name){
    m_name = name;
}

AED_RenderComp* AED_Archtype::GetRenderComp(){
    return &m_rendercomp;
}

AED_KeyboardComp* AED_Archtype::GetKeyboardComp(){
    return &m_keyboardcomp;
}

AED_PhysicsComp* AED_Archtype::GetPhysicsComp(){
    return &m_physicscomp;
}

AED_AlwaysComp* AED_Archtype::GetAlwaysComp(){
    return &m_alwayscomp;
}

AED_MouseComp* AED_Archtype::GetMouseComp(){
    return &m_mousecomp;
}

AED_SoundComp* AED_Archtype::GetSoundComp(){
    return &m_soundcomp;
}

AED_ParticleSystemComp* AED_Archtype::GetParSysComp(){
    return &m_parsyscomp;
}

AED_ParticleComp* AED_Archtype::GetParticleComp(){
    return &m_particlecomp;
}

AED_Archtype& AED_Archtype::operator=(const AED_Archtype &arch){
    m_name = arch.m_name;
    m_rendercomp = arch.m_rendercomp;
    m_keyboardcomp = arch.m_keyboardcomp;
    m_physicscomp = arch.m_physicscomp;
    m_alwayscomp = arch.m_alwayscomp;
    m_mousecomp = arch.m_mousecomp;
    m_parsyscomp = arch.m_parsyscomp;
    m_particlecomp = arch.m_particlecomp;
    m_soundcomp = arch.m_soundcomp;

    m_triggers = arch.m_triggers;
    m_attributes = arch.m_attributes;


    return *this;
}

void AED_Archtype::Save(QTextStream &file){
    /*
    file << "<archtype>" << endl;
    file << "   <name = " << m_name << ">" << endl;
    m_rendercomp.Save(file);
    m_physicscomp.Save(file);
    m_keyboardcomp.Save(file);
    m_mousecomp.Save(file);
    m_alwayscomp.Save(file);
    m_parsyscomp.Save(file);
    m_particlecomp.Save(file);


    for(int i=0;i<m_triggers.size();i++){
        if(!m_triggers[i].GetConditions()->isEmpty()){
            file <<  "   <trigger>" << endl;
            file <<  "      <script = " << m_triggers[i].GetScript() << ">"<<endl;
            for(int j=0;j<m_triggers[i].GetConditions()->size();j++){
                file<<"      <condition>" << endl;
                file<<"         <type = " << m_triggers[i].GetConditions()->operator [](j).type << ">"<< endl;
                file<<"         <name = " << m_triggers[i].GetConditions()->operator [](j).name << ">"<< endl;
                file<<"      </condition>" << endl;
            }
            file <<  "   </trigger>" << endl;
        }
    }
    for(int i=0;i<m_attributes.size();i++){
        file <<  "   <attribute>" << endl;
        file<<"         <type = " << m_attributes[i].GetType() << ">"<< endl;
        file<<"         <name = " << m_attributes[i].GetName() << ">"<< endl;
        file <<  "   </attribute>" << endl;
    }
    file << "</archtype>"<<endl<<endl<<endl;*/
}

void AED_Archtype::Save(QDomDocument &doc, QDomElement &header){
    QDomElement elem = doc.createElement("archtype");
    elem.setAttribute("name",m_name);
    m_rendercomp.Save(doc,elem);
    m_physicscomp.Save(doc,elem);
    m_keyboardcomp.Save(doc,elem);
    m_mousecomp.Save(doc,elem);
    m_alwayscomp.Save(doc,elem);
    m_soundcomp.Save(doc,elem);
    m_parsyscomp.Save(doc,elem);
    m_particlecomp.Save(doc,elem);

    for(int i=0;i<m_triggers.size();i++){
        if(!m_triggers[i].GetConditions()->isEmpty()){
            QDomElement trig = doc.createElement("trigger");
            trig.setAttribute("script",m_triggers[i].GetScript()->GetPath());

            for(int j=0;j<m_triggers[i].GetConditions()->size();j++){
                QDomElement cond = doc.createElement("condition");
                cond.setAttribute("type", m_triggers[i].GetConditions()->operator [](j).type);
                cond.setAttribute("name", m_triggers[i].GetConditions()->operator [](j).name);
                trig.appendChild(cond);
            }
            elem.appendChild(trig);
        }
    }

    for(int i=0;i<m_attributes.size();i++){
        QDomElement attr = doc.createElement("attribute");
        attr.setAttribute("type",m_attributes[i].GetType());
        attr.setAttribute("name",m_attributes[i].GetName());
        elem.appendChild(attr);
    }
    header.appendChild(elem);

}

bool AED_Archtype::Load(QDomElement &aelem, ImageManager &imgmngr, ScriptModel &scrmdl){
    QDomNodeList comps = aelem.elementsByTagName("component");
    for(int i=0;i<comps.count();i++){
        QDomElement celem = comps.at(i).toElement();
        QString type = celem.attribute("comptype");
        if(type=="Always")
            m_alwayscomp.Load(celem);
        else if(type=="Keyboard")
            m_keyboardcomp.Load(celem);
        else if(type=="Mouse")
            m_mousecomp.Load(celem);
        else if(type=="Sound")
            m_soundcomp.Load(celem);
        else if(type=="Particle")
            m_particlecomp.Load(celem);
        else if(type=="ParticleSystem")
            m_parsyscomp.Load(celem);
        else if(type=="Physics")
            m_physicscomp.Load(celem);
        else if(type=="Render")
            m_rendercomp.Load(celem, imgmngr);
    }
    QDomNodeList triggers = aelem.elementsByTagName("trigger");
    for(int i=0;i<triggers.count();i++){
        QDomElement telem = triggers.at(i).toElement();
        AED_Trigger trigger;
        trigger.SetScript(scrmdl.GetScript(telem.attribute("script")));
        QDomNodeList conds = telem.elementsByTagName("condition");
        for(int j=0;j<conds.count();j++){
            QDomElement celem = conds.at(j).toElement();
            QString type = celem.attribute("type");
            QString name = celem.attribute("name");
            trigger.AddCondition(type,name);
        }

        m_triggers.append(trigger);
    }

    QDomNodeList attrs = aelem.elementsByTagName("attribute");
    for(int i=0;i<attrs.count();i++){
        QDomElement aelem = attrs.at(i).toElement();
        QString type = aelem.attribute("type");
        QString name = aelem.attribute("name");
        AED_Attribute attr(type,name);

        m_attributes.append(attr);
    }


    return true;
}
